const $api = require('../../network/api')
Page({
    data: {
        show: false,
        account: '',
        password: '',
        show: false,
        user: {},
        avatar: 'http://rjt1lcc7r.hn-bkt.clouddn.com/c326e7bb19f91bda07a54fa566d616f.jpg',
        icon: 'http://rjt1lcc7r.hn-bkt.clouddn.com/%E5%BE%AE%E4%BF%A1.png'

    },
    onShow() {
        const user = wx.getStorageSync('userInfo')
        console.log(user, 'user');
        this.setData({
            user: user
        })
    },
    async login() {
        this.setData({
            show: true
        })
        if (!this.data.account && !this.data.password) {
            return wx.showToast({
                title: '账号密码不能为空！',
                duration: 2000,
                icon: 'none'
            })
        }
        const param = {
            account: this.data.account,
            password: this.data.password
        }
        const result = await $api.Login(param)
        console.log(result, 'sss');
        if (result.code == 200) {
            this.showToast(result.msg)
            const { token } = result.data
            wx.setStorageSync('ACCESS_TOKEN', token)
            wx.setStorageSync('userInfo', result.data.userInfo)
            this.setData({
                show: false,
                user: result.data.userInfo
            })
        }
    },
    // 微信一键登录
    async wxLogin() {
        wx.getUserProfile({
            desc: '拿到个人信息用户注册昵称',
            success: async(res) => {
                const { avatarUrl, nickName } = res.userInfo
                    // console.log(avatarUrl, nickName);
                const res1 = await this.getCode()
                console.log(res1);
                const param = {
                    avatar: avatarUrl,
                    username: nickName,
                    code: res1.code
                }
                const res2 = await $api.wxLogin(param)
                if (res2.code == 200) {
                    console.log(res2);
                    this.showToast(res2.msg)
                    const { token } = res2.data
                    wx.setStorageSync('ACCESS_TOKEN', token)
                    wx.setStorageSync('userInfo', res2.data.userInfo)
                    this.setData({
                        show: false,
                        user: res2.data.userInfo
                    })

                }
            },
            fail: (res) => {
                console.log(res, 'res');
            }
        })

    },
    // 获取微信验证code
    async getCode() {
        return new Promise((resole, reject) => {
            wx.login({
                success: (res) => {
                    resole(res)
                },
                fail: (res) => {
                    reject(res)
                }
            })
        })

    },
    showToast(title, icon = 'none', duration = 2500, mask = false) {
        wx.showToast({
            title: title || '',
            icon,
            duration,
            mask
        });
    },
    async onLogin() {
        if (Object.keys(this.data.user).length == 0) {
            this.setData({
                show: true
            })
        }

    },
    // 退出登录
    exitOut() {
        wx.removeStorageSync('userInfo')
        wx.removeStorageSync('ACCESS_TOKEN')
        this.setData({
            user: {}
        })
        this.showToast('退出登录成功')

    },
    onClose() {
        this.setData({ show: false });
    }

})